package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.material.textfield.TextInputEditText;

public class SqlActivity extends AppCompatActivity {
    private TextInputEditText edit_onesno,edit_onename,edit_onesex,edit_onepro,edit_onedep;;
    private Button btn_oneadd,btn_oneclear,btn_onenext;
    private MyDBOpenHelper mhelper;
    private SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sql_add);
        initView();
        btnAdd();
    }
    private void initView() {
        edit_onesno=findViewById(R.id.editText_onesno);
        edit_onename=findViewById(R.id.editText_onename);
        edit_onesex=findViewById(R.id.editText_onesex);
        edit_onepro=findViewById(R.id.editText_onepro);
        edit_onedep=findViewById(R.id.editText_onedep);
        btn_oneadd=findViewById(R.id.button_oneadd);
        btn_oneclear=findViewById(R.id.button_oneclear);
        btn_onenext=findViewById(R.id.button_onenext);
        mhelper=new MyDBOpenHelper(SqlActivity.this);
        db=mhelper.getWritableDatabase();
    }
    private void btnAdd() {
        btn_oneadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContentValues values=new ContentValues();
                values.put("sno",edit_onesno.getText().toString());
                values.put("name",edit_onename.getText().toString());
                values.put("sex",edit_onesex.getText().toString());
                values.put("professional",edit_onepro.getText().toString());
                values.put("deparment",edit_onedep.getText().toString());

                db.insert("stu_info",null,values);
                Toast.makeText(SqlActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
            }
        });
    }
    private void btnClearNext() {
        //清除按钮的功能
        btn_oneclear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edit_onesno.setText("");
                edit_onename.setText("");
                edit_onesex.setText("");
                edit_onepro.setText("");
                edit_onedep.setText("");
            }
        });
        btn_onenext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(SqlActivity.this,SqlSecondActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    }